<HTML> 
<HEAD> 
	<TITLE>3rd Party ROBOTC Drivers</TITLE> 
<link href="tabs.css" rel="stylesheet" type="text/css"/> 
<link href="doxygen.css" rel="stylesheet" type="text/css"/> 
</HEAD> 
 
<BODY bgcolor="#ffffff" link="#000000" vlink="#000000"> 
<table width="100%" bgcolor="navy" cellspacing=0 cellpadding=1 border=0> 
<tr><td><table width="100%" bgcolor="#EEEEDD" cellspacing=0 cellpadding=3 border=0> 
<tr> 
    <td width="33%" align="left"> <img src="images/LOGO_NXT.gif" width=266 height=44 border="0" alt="Mindstorms"></td> 
    <td><b><font size="+3" color="navy">3rd Party ROBOTC Drivers</font></b></td> 
    <td align="right"> <img src="images/logo.png" width=44 height=44 border="0" alt="RobotC"></td>     
</tr> 
  <tr bgcolor="#cccc99" align="right"> 
          <td colspan=3> <font face="arial" size="-1"> [<a href=main.html>Home</a>] [<a target=_top href="https://sourceforge.net/projects/rdpartyrobotcdr/">Download</a>] [<a target=_top href="http://apps.sourceforge.net/mantisbt/rdpartyrobotcdr/my_view_page.php">Submit a bug/suggestion</a>]  [<a target=_top href="http://www.robotc.net/forums/">ROBOTC Forums</a>] [<a target=_top href="http://botbench.com">Blog</a>] [<a target=_top href="http://sourceforge.net/donate/index.php?group_id=257238">Support this project</a>]</font> </td> 
</tr> 
</table></td></tr> 
</table> 
<!-- Generated by Doxygen 1.7.2 -->
<div class="header">
  <div class="headertitle">
<h1>benedettelli-nxt2wifi-test2.c</h1>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><span class="preprocessor">#pragma config(Sensor, S4,     NXT2WIFI,       sensorHighSpeed)</span>
<span class="preprocessor"></span><span class="comment">//*!!Code automatically generated by &#39;ROBOTC&#39; configuration wizard               !!*//</span>

<span class="comment">/*</span>
<span class="comment"> * $Id: benedettelli-nxt2wifi-test2.c 133 2013-03-10 15:15:38Z xander $</span>
<span class="comment"> */</span>
<span class="comment"></span>
<span class="comment">/**</span>
<span class="comment"> * benedettelli-nxt2wifi.h provides an API for Daniele&#39;s NXT2WiFi Sensor. This program</span>
<span class="comment"> * demonstrates how to use that API.  This program is opens a connection to a BOFH server</span>
<span class="comment"> * and fetches an excuse and displays it on the screen.  It is a demo for opening a TCP socket</span>
<span class="comment"> * to a remote host.</span>
<span class="comment"> *</span>
<span class="comment"> * Changelog:</span>
<span class="comment"> * - 0.1: Initial release</span>
<span class="comment"> *</span>
<span class="comment"> * Credits:</span>
<span class="comment"> * - Big thanks to Daniele Benedettelli for providing me with the hardware necessary to write and test this.</span>
<span class="comment"> *</span>
<span class="comment"> * License: You may use this code as you wish, provided you give credit where it&#39;s due.</span>
<span class="comment"> *</span>
<span class="comment"> * THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.59 AND HIGHER. </span>
<span class="comment"></span>
<span class="comment"> * Xander Soldaat (xander_at_botbench.com)</span>
<span class="comment"> * 22 July 2012</span>
<span class="comment"> * version 0.1</span>
<span class="comment"> */</span>

<span class="preprocessor">#include &quot;<a class="code" href="common_8h.html" title="Commonly used functions used by drivers.">drivers/common.h</a>&quot;</span>
<span class="preprocessor">#include &quot;<a class="code" href="benedettelli-nxt2wifi_8h.html" title="Dani&amp;#39;s WiFi Sensor.">drivers/benedettelli-nxt2wifi.h</a>&quot;</span>

<span class="keywordtype">long</span> txbytes = 0;
<span class="keywordtype">long</span> rxbytes = 0;
<span class="keywordtype">string</span> IPaddress = <span class="stringliteral">&quot;0.0.0.0&quot;</span>;
<span class="keywordtype">string</span> connStatus = <span class="stringliteral">&quot;disconnected&quot;</span>;
<span class="keywordtype">string</span> dataStrings[4];
<a name="a0"></a><a class="code" href="group__common.html#gacc2928041c5ff8e7f2ddef1e88431e80">tHugeByteArray</a> data;
<span class="keywordtype">int</span> sizeOfReceivedData;

task updateScreen()
{
  <span class="keywordflow">while</span>(<span class="keyword">true</span>)
  {
    nxtDisplayTextLine(0, <span class="stringliteral">&quot;Stat: %s&quot;</span>, connStatus);
    nxtDisplayTextLine(1, <span class="stringliteral">&quot;%s&quot;</span>,IPaddress);
    nxtDisplayTextLine(2, <span class="stringliteral">&quot;-------------------&quot;</span>);
    nxtDisplayTextLine(3, <span class="stringliteral">&quot;%s&quot;</span>, dataStrings[0]);
                nxtDisplayTextLine(4, <span class="stringliteral">&quot;%s&quot;</span>, dataStrings[1]);
                nxtDisplayTextLine(5, <span class="stringliteral">&quot;%s&quot;</span>, dataStrings[2]);
                nxtDisplayTextLine(6, <span class="stringliteral">&quot;%s&quot;</span>, dataStrings[3]);
    nxtDisplayTextLine(7, <span class="stringliteral">&quot;RX/TX: %d/%d&quot;</span>, rxbytes, txbytes);
    wait1Msec(100);
  }
}

<span class="keywordtype">void</span> processData()
{
  <span class="comment">// Used for going through the current line so we</span>
  <span class="comment">// can print it</span>
  <span class="keywordtype">string</span> line = <span class="stringliteral">&quot;&quot;</span>;
  <span class="keywordtype">string</span> singleword = <span class="stringliteral">&quot;&quot;</span>;

        <span class="comment">// What line are we current printing on?</span>
  <span class="keywordtype">int</span> screenLine = 0;

  dataStrings[0] = <span class="stringliteral">&quot;&quot;</span>;
  dataStrings[1] = <span class="stringliteral">&quot;&quot;</span>;
  dataStrings[2] = <span class="stringliteral">&quot;&quot;</span>;
  dataStrings[3] = <span class="stringliteral">&quot;&quot;</span>;
  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; sizeOfReceivedData; i++)
  {
    <span class="comment">// It&#39;s a printable character</span>
    <span class="keywordflow">if</span> (<a name="a1"></a><a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[i] &gt; 0x20)
    {
      <span class="comment">// Append to the current word</span>
      strncat(singleword, &amp;<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[i], 1);
    }
    <span class="comment">// we&#39;ve reached a word or line boundary</span>
    <span class="keywordflow">else</span> <span class="keywordflow">if</span> ((<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[i] == 0x20) || (<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[i] == 0x0A))
    {
      <span class="comment">// is the current word and the current line more than we can print?</span>
      <span class="keywordflow">if</span> ((strlen(line) + strlen(singleword)) &gt; 16)
      {
        <span class="comment">// print out the current line</span>
        dataStrings[screenLine++] = line;
        <span class="comment">// current word becomes new line</span>
        memcpy(line, singleword, <span class="keyword">sizeof</span>(singleword));
        <span class="comment">// blank out the current word</span>
        singleword = <span class="stringliteral">&quot;&quot;</span>;
      }
      <span class="keywordflow">else</span>
      {
        <span class="comment">// Append a space to the current line before sticking</span>
        <span class="comment">// the new word to it</span>
        <span class="keywordflow">if</span> (strlen(line) &gt; 0)
          strcat(line, <span class="stringliteral">&quot; &quot;</span>);
        strcat(line, singleword);
        singleword = <span class="stringliteral">&quot;&quot;</span>;
      }

      <span class="comment">// Are we at the end of the line and need to print it out regardless?</span>
      <span class="keywordflow">if</span> (<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[i] == 0x0A)
      {
        dataStrings[screenLine++] = line;
        <span class="comment">// Reset the current line</span>
        line = <span class="stringliteral">&quot;&quot;</span>;
      }
    }
  }
}
task main ()
{
  <span class="keywordtype">string</span> BOFHserver = <span class="stringliteral">&quot;192.168.0.100&quot;</span>; <span class="comment">// Linux VM</span>
  <span class="keywordtype">int</span> BOFHport = 6666;
  <span class="keywordtype">string</span> dataString;
  getFriendlyName(dataString);

  <span class="keywordtype">int</span> avail = 0;

  nNxtButtonTask = -2;
  StartTask(updateScreen);

  <span class="comment">// initialise the port, etc</span>
  <a name="a2"></a><a class="code" href="group__common-rs485.html#ga681eadb60516210bbd261bc50d80dab7">RS485initLib</a>();

  memset(<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>, 0, <span class="keyword">sizeof</span>(<a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>));
  memset(<a name="a3"></a><a class="code" href="group__common-rs485.html#ga76b40f6d5142bd650f910cd6cd19c588">RS485txbuffer</a>, 0, <span class="keyword">sizeof</span>(<a class="code" href="group__common-rs485.html#ga76b40f6d5142bd650f910cd6cd19c588">RS485txbuffer</a>));

  <span class="comment">// Disconnect if already connected</span>
  <a name="a4"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga8ec5d0f6add0e5d953ad690f109a3253">N2WDisconnect</a>();
  <a name="a5"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga8cad34ee07c9a294819f0070c94e1720">N2WchillOut</a>();

  <span class="keywordflow">if</span> (!<a name="a6"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga518cfe843bc8f4d042405227a0a2f4ff">N2WCustomExist</a>())
  {
    StopTask(updateScreen);
    wait1Msec(50);
    eraseDisplay();
    PlaySound(soundException);
    nxtDisplayCenteredBigTextLine(1, <span class="stringliteral">&quot;ERROR&quot;</span>);
    nxtDisplayTextLine(3, <span class="stringliteral">&quot;No custom profile&quot;</span>);
    nxtDisplayTextLine(4, <span class="stringliteral">&quot;configured!!&quot;</span>);
    <span class="keywordflow">while</span>(<span class="keyword">true</span>) EndTimeSlice();
  }

  <a name="a7"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#gac54a69b33b3a29473e8b4e80f993a401">N2WLoad</a>();

  wait1Msec(100);
  <a name="a8"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#gaa0c803e53ea5cd1221aed19eea5463f4">N2WConnect</a>(<span class="keyword">true</span>);
  connStatus = <span class="stringliteral">&quot;connecting&quot;</span>;

  <span class="keywordflow">while</span> (!<a name="a9"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga2012c5479768124f2083dc643d7b4573">N2WConnected</a>())
    wait1Msec(1000);

  connStatus = <span class="stringliteral">&quot;connected&quot;</span>;
  PlaySound(soundBeepBeep);

  wait1Msec(3000);
  <a name="a10"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga75f932c01ab7aaf3ba965179b2773215">N2WgetIP</a>(IPaddress);

  wait1Msec(1000);


  <span class="keywordflow">while</span> (<span class="keyword">true</span>)
  {
    <span class="keywordflow">while</span> (nNxtButtonPressed != kEnterButton) EndTimeSlice();
    <span class="keywordflow">while</span> (nNxtButtonPressed != kNoButton) EndTimeSlice();
    <span class="keywordflow">if</span> (<a name="a11"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga6a0b83afc5568dafe0cc76020e769a87">N2WTCPOpenClient</a>(1, BOFHserver, BOFHport)) {
      data[0] = 0x0A;
      <a name="a12"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#gabaa4672cbcc39d98359ae5926e6fa852">N2WTCPWrite</a>(1, data, 1);
      txbytes++;
      <span class="comment">// How many bytes are available in the buffers?</span>
            avail = <a name="a13"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#gad03127c6ae3da843d91f77e3bb27bb31">N2WTCPAvail</a>(1);
            <span class="keywordflow">if</span> (avail &gt; 0)
            {
              sizeOfReceivedData = avail;
              rxbytes += avail;
              <a class="code" href="group___n_x_t2_w_i_f_i.html#ga8cad34ee07c9a294819f0070c94e1720">N2WchillOut</a>();
              <span class="comment">// read the current buffer</span>
              <a name="a14"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#gadb790227d5aae36490f830db97b5e85e">N2WTCPRead</a>(1, avail);
              processData();
              <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; avail; i++)
              {
                writeDebugStream(<span class="stringliteral">&quot;%c&quot;</span>, <a class="code" href="group__common-rs485.html#gae1645d814439623db869c5bfb5d4bd32">RS485rxbuffer</a>[i]);
              }
              writeDebugStreamLine(<span class="stringliteral">&quot;&quot;</span>);
            }
            <span class="comment">// Wait a bit</span>
      <a class="code" href="group___n_x_t2_w_i_f_i.html#ga8cad34ee07c9a294819f0070c94e1720">N2WchillOut</a>();
      <span class="comment">// Disconnect</span>
      <a name="a15"></a><a class="code" href="group___n_x_t2_w_i_f_i.html#ga83d5a491bba4318f2b41b6eeebbc95df">N2WTCPClose</a>(1);
    }
  }
}
</pre></div> </div>
</div>
<hr class="footer"/><address class="footer"><small>Generated on Wed Mar 13 2013 19:56:41 for ROBOTC Drivers by&#160;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.2 </small></address>
</body>
</html>
